package com.lune.job.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.framework.role.RequireRoles;
import com.lune.job.framework.role.Role;
import com.lune.job.pojo.*;
import com.lune.job.service.*;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/send")
@Api(tags = "投递管理")
public class SendController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IUserDataService userDataService;
    @Autowired
    private ISendService sendService;
    @Autowired
    private IResumeService resumeService;

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IPostService postService;

    @Autowired
    private IStudentService studentService;


    @PostMapping("/")
    @ApiOperation(value = "添加投递")
    public RespBean create(@RequestBody Map<String, Integer> map) {
        UserData userData = userDataService.getUserData();
        //1、判断用户是否登录
        if (userData == null) {
            return RespBean.error("未登录不能操作");
        }
        Integer post_id = map.get("post_id");
        Integer company_id = map.get("company_id");

        //2、判断是否重复投递简历
        Send sendParam = new Send();
        sendParam.setStudent_id(userData.getId());
        sendParam.setPost_id(post_id);
        sendParam.setCompany_id(company_id);
        List<Send> sends = sendService.queryByCon(sendParam);
        if (sends.size() > 0 && sends != null) {
            return RespBean.error("请勿重复投递简历");
        }

        Send send = new Send();
        send.setPost_id(post_id);
        send.setCompany_id(company_id);
        send.setSend_date(new Date());
        send.setStatus(0);
        send.setStudent_id(userData.getId());

        //3、判断简历是否开放
        Resume resume = new Resume();
        resume.setStudent_id(userData.getId());
        resume.setStatus(1);
        List<Resume> resumeList = resumeService.queryByCon(resume);
        if (resumeList.size() > 0 && resumeList != null) {
            Resume entity = resumeList.get(0);
            send.setResume_id(entity.getId());
        } else {
            return RespBean.error("请完善简历");
        }

        //4、投递
        boolean save = sendService.save(send);
        if (save) {
            return RespBean.success("投递成功");

        } else {
            return RespBean.error("投递失败");

        }

    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除投递记录")
    public RespBean delete(@PathVariable Integer id) {
        if (sendService.removeById(id)) {
            return RespBean.success("删除投递记录成功");
        } else {
            return RespBean.error("删除投递记录失败");
        }
    }

    @DeleteMapping("/")
    @ApiOperation(value = "批量删除投递记录")
    public RespBean deleteByIds(Integer[] ids) {
        if (sendService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除投递记录成功");
        } else {
            return RespBean.error("批量删除投递记录失败");
        }
    }

    @PutMapping("/")
    @ApiOperation(value = "修改投递记录")
    public RespBean update(@RequestBody Send send) {
        if (sendService.updateById(send)) {
            return RespBean.success("修改投递记录成功");
        } else {
            return RespBean.error("修改投递记录失败");
        }
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "根据修改投递记录")
    public RespBean updateStatus(@RequestBody Send send) {
        if (sendService.updateStatus(send)) {
            return RespBean.success("修改投递记录成功");
        } else {
            return RespBean.error("修改投递记录失败");
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询投递记录")
    public RespBean detail(Integer id) {
        Send byId = sendService.getById(id);
        if (byId != null) {
            return RespBean.success("查询成功", byId);
        } else {
            return RespBean.error("查询失败");
        }
    }

    @RequireRoles(type = Role.COMPANY)
    @ApiOperation(value = "公司-分页查询投递记录")
    @PostMapping("/queryPageCompany")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 @RequestBody Send send) {
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        send.setCompany_id(userData.getId());
        IPage<Send> pageInfo = sendService.queryPage(currentPage, size, send);
        pageInfo.getRecords().forEach(item -> {
//            if (item.getCompany_id() == userData.getId()) {
                item.setCompany(companyService.getById(item.getCompany_id()));
                item.setResume(resumeService.getById(item.getResume_id()));
                item.setStudent(studentService.getById(item.getStudent_id()));
                item.setPost(postService.getById(item.getPost_id()));
//            }

        });
        return new ResPageBean(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getRecords());
    }

    @ApiOperation(value = "条件查询投递记录")
    @PostMapping("/queryCon")
    public List<Send> queryByCon(@RequestBody Send send) {
        return sendService.queryByCon(send);
    }

    @RequireRoles(type = Role.STUDENT)
    @ApiOperation(value = "学生-分页查询投递记录")
    @PostMapping("/queryPageStu")
    public ResPageBean queryPageStu(@RequestParam(defaultValue = "1") Integer currentPage,
                                    @RequestParam(defaultValue = "10") Integer size,
                                    @RequestBody Send send) {
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        send.setStudent_id(userData.getId());
        IPage<Send> pageInfo = sendService.queryPage(currentPage, size, send);
        pageInfo.getRecords().forEach(item -> {

            item.setCompany(companyService.getById(item.getCompany_id()));
            item.setResume(resumeService.getById(item.getResume_id()));
            item.setPost(postService.getById(item.getPost_id()));


        });
        return new ResPageBean(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getRecords());
    }

}
